#include "sendvolumelist.h"
#include "ProtocalIdentifier.h"
#include "NetworkCommonFunction.h"

#include <QDomElement>
#include <QDebug>

SendVolumeList::SendVolumeList(int windowNo, const std::vector<VolumeNode> &volumePlan):
		SenderCommand(GetProtocalIdentifier("SendVolumeList"))
		,m_WindowNo(windowNo)
{
	QDomElement rootElement = m_VolumeXml.createElement("VolumeList");
	rootElement.setAttribute("WindowNo", m_WindowNo);
	m_VolumeXml.appendChild(rootElement);
	for(size_t i = 0; i < volumePlan.size(); ++i)
	{
		const VolumeNode& volumeNode = volumePlan[i];
		QDomElement element = m_VolumeXml.createElement("VolumeP");
		rootElement.appendChild(element);
		element.setAttribute("StartTime", volumeNode.startTime.toString("hh:mm"));
		element.setAttribute("EndTime", volumeNode.endTime.toString("hh:mm"));
		element.setAttribute("Percentage", volumeNode.percentage);
	}
	qDebug()<<m_VolumeXml.toByteArray();
}

void SendVolumeList::constructCommandData(QByteArray &commandData)
{
	unsigned long commandType = m_CommandType;
	commandData.append(FBtoNetworkCharP(commandType), 4);

	QByteArray byteData = m_VolumeXml.toByteArray();
	unsigned long xmlLength = byteData.length();

	unsigned long commandLength = sizeof(xmlLength) + byteData.length();

	commandData.append(FBtoNetworkCharP(commandLength), 4);
	commandData.append(FBtoNetworkCharP(xmlLength), 4);
	commandData.append(byteData);
}
